import fs, { readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { resolve, extname } from 'node:path';
// 获取配置文件
const CONFIG_MAP = readdirSync(resolve(process.cwd(), 'config')).reduce((map, fileName) => {
    //文本解析
    try {
        const ext = extname(fileName).slice(1);
        const [, year] = fileName.match(/^(\d{4})\./);
        const fileContent = readFileSync(
            resolve(process.cwd(), 'config', fileName)
        ).toString();
        if (ext === 'txt') {
            const holidayList = [], addWorkDayList = [];
            map[year] = { holidayList, addWorkDayList }
            //加工放假记录
            try {
                let index = 0;
                const txtRG = /[^\d]*(?:\d{4}年){0,1}(\d{1,2})月(\d{1,2})日.*共(\d*)天。/;
                while (txtRG.test(fileContent.slice(index))) {
                    const [resStr, m, d, time] = fileContent.slice(index).match(txtRG);
                    holidayList.push({
                        start: `${year}-${m}-${d}`,
                        time
                    })
                    //更新起始点
                    index = fileContent.indexOf(resStr) + resStr.length;
                }
            } catch (err) {
                console.error(err);
            };
            // 加工调休记录
            try {
                let index = 0;
                const txtRG = /(?:(?:。|、)(\d{1,2})月(\d{1,2})日)/;
                while (txtRG.test(fileContent.slice(index))) {
                    const [resStr, m, d] = fileContent.slice(index).match(txtRG);
                    addWorkDayList.push({
                        start: `${year}-${m}-${d}`,
                        time: 1
                    })
                    //更新起始点
                    index = fileContent.indexOf(resStr) + resStr.length;
                }
            } catch (err) {
                console.error(err)
            }
        }
        if (ext === 'json')
            map[year] = JSON.parse(fileContent);
    } catch (err) {
        console.error(err);
    }
    return map;
}, {});

const RESULT_MAP_JSON = Object.entries(CONFIG_MAP).reduce(function (jsonString, [year, { holidayList, addWorkDayList }]) {
    try {
        const holidayMap = holidayList.reduce(function (map, { start, time }) {
            map[start] = time;
            return map;
        }, {});
        // 定义调休天数
        const workdayMap = addWorkDayList.reduce(function (map, { start, time }) {
            map[start] = time;
            return map;
        }, {});
        return jsonString.concat("\n    ",`"${year}":${JSON.stringify(getCurrentYearDays(year, holidayMap, workdayMap))},`)
    } catch (err) {
        console.error(err);
    }
    return jsonString;
}, "{").slice(0, -1) + "\n}";

writeFileSync('./holidays.json', RESULT_MAP_JSON);

function getCurrentYearDays(year, holidayMap, workdayMap) {
    const holidays = {};
    let time = 0, flag = 0;
    for (let m = 1; m <= 12; m++) {
        for (let d = 1; d <= getMonthDays(m, 2022); d++) {
            const date = `${year}-${m}-${d}`;
            const day = new Date(date).getDay();
            if (time-- > 0) {
                holidays[`${m},${d}`] = flag;
                continue;
            }
            if (day === 6 || day === 0) {
                holidays[`${m},${d}`] = flag = 3;
            }
            //周末加班
            if (workdayMap[date] && flag === 3) {
                holidays[`${m},${d}`] = flag = 2;
                time = --workdayMap[date];
                continue;
            }
            //节假日休息
            if (holidayMap[date]) {
                holidays[`${m},${d}`] = flag = 1;
                time = --holidayMap[date];
            }
        }
    }
    return holidays;
}

function getMonthDays(month, year) {
    if (month === 2) {
        if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
            return 29
        }
        return 28
    }
    if (month < 8) {
        return month % 2 === 1 ? 31 : 30
    }
    return month % 2 === 0 ? 31 : 30
}